package com.example.k8s.hello.service.sys.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.example.k8s.hello.service.sys.service.SysRoleMenuService;
import com.example.k8s.hello.service.sys.service.SysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import com.example.k8s.hello.common.annotation.SysLog;
import com.example.k8s.hello.common.bean.Page;
import com.example.k8s.hello.common.utils.StpExtendUtil;
import com.example.k8s.hello.bean.sys.entity.SysRoleEntity;
import com.example.k8s.hello.bean.sys.query.SysRoleQuery;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色管理
 *
 * @author Mark sunlightcs@gmail.com
 */
@Tag(name = "角色管理")
@RestController
@RequestMapping("/sys/role")
@RequiredArgsConstructor
public class SysRoleController {
	private final SysRoleService sysRoleService;
	private final SysRoleMenuService sysRoleMenuService;

	/**
	 * 角色分页
	 */
	@Operation(summary = "角色分页")
	@GetMapping("/list")
	@SaCheckPermission("sys:role:list")
	public Page list(@ParameterObject SysRoleQuery query, @ParameterObject Page page){
		return sysRoleService.queryPage(query, page);
	}
	
	/**
	 * 角色列表
	 */
	@Operation(summary = "角色列表")
	@GetMapping("/select")
	@SaCheckPermission("sys:role:select")
	public List<SysRoleEntity> select(){
		List<SysRoleEntity> list = sysRoleService.list(
//				Wrappers.lambdaQuery(SysRoleEntity.class)
//						.eq(!StpExtendUtil.ifAdmin(), SysRoleEntity::getCreateUserId, StpExtendUtil.getUserId())
		);
		
		return list;
	}
	
	/**
	 * 角色详情
	 */
	@Operation(summary = "角色详情")
	@GetMapping("/info/{roleId}")
	@SaCheckPermission("sys:role:info")
	public SysRoleEntity info(@PathVariable("roleId") Long roleId){
		SysRoleEntity role = sysRoleService.getById(roleId);
		
		//查询角色对应的菜单
		List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
		role.setMenuIdList(menuIdList);
		
		return role;
	}
	
	/**
	 * 保存角色
	 */
	@Operation(summary = "保存角色")
	@SysLog("保存角色")
	@PostMapping("/save")
	@SaCheckPermission("sys:role:save")
	public void save(@RequestBody @Validated SysRoleEntity role){
		sysRoleService.saveRole(role);
	}
	
	/**
	 * 修改角色
	 */
	@Operation(summary = "修改角色")
	@SysLog("修改角色")
	@PostMapping("/update")
	@SaCheckPermission("sys:role:update")
	public void update(@RequestBody @Validated SysRoleEntity role){
		role.setCreateUserId(StpExtendUtil.getUserId());
		sysRoleService.update(role);
	}
	
	/**
	 * 删除角色
	 */
	@Operation(summary = "删除角色")
	@SysLog("删除角色")
	@PostMapping("/delete")
	@SaCheckPermission("sys:role:delete")
	public void delete(@RequestBody Long[] roleIds){
		sysRoleService.deleteBatch(roleIds);
	}
}
